package com.bysc.commentservice.service.impl;

import com.bysc.commonutils.enums.CommentEnum;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commentservice.entity.vo.LikeVo;
import com.bysc.commentservice.service.GoodLikeApiService;
import com.bysc.commentservice.utils.SendMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.concurrent.ThreadPoolExecutor;

import static com.bysc.commonutils.constant.RedisConstant.*;

/**
 * 点赞服务实现类
 */
@Service
@Slf4j
public class GoodLikeApiServiceImpl implements GoodLikeApiService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private SendMessageService sendMessageService;
    /**
     * 处理点赞操作
     *
     * @param likeVo  点赞信息
     * @param request HTTP请求对象，用于获取用户ID
     */
    @Override
    public void likeByToken(LikeVo likeVo, HttpServletRequest request) {
        // 从JWT令牌中获取用户ID
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        String id = likeVo.getId();
        HashOperations hash = redisTemplate.opsForHash();
        SetOperations set = redisTemplate.opsForSet();
        String hashKey = "";
        String setKey = "";
        // 根据点赞类型获取对应的枚举对象
        CommentEnum commentEnum = CommentEnum.getByValue(likeVo.getType());
        switch (commentEnum) {
            case POST:
                log.info("处理帖子点赞,用户：{}-{}", userId, id);
                hashKey = COMMENT_POST + id;
                setKey = COMMENT_POST_USER_LIKE + userId;
                break;
            case PARENT:
                log.info("处理父评论点赞,用户：{}-{}", userId, id);
                hashKey = COMMENT_PARENTCOMMENT + id;
                setKey = COMMENT_PARENTCOMMENT_USER_LIKE + userId;
                break;
            case SON:
                log.info("处理子评论点赞,用户：{}-{}", userId, id);
                hashKey = COMMENT_SONCOMMENT + id;
                setKey = COMMENT_SONCOMMENT_USER_LIKE + userId;
                break;
            case STUDYMATERIAL:
                log.info("处理学习资料点赞,用户：{}-{}", userId, id);
                hashKey = STUDY_DATA + id;
                setKey = STUDY_DATA_USER_LIKE + userId;
                break;
        }
        // 执行点赞或取消点赞操作
        doLike(hash, set, userId, id, hashKey, setKey, commentEnum);
    }

    /**
     * 处理点赞业务
     *
     * @param hash      Redis操作Hash对象
     * @param set       Redis操作Set对象
     * @param userId    点赞用户ID
     * @param id        点赞内容ID
     * @param hashKey   Hash操作的Key
     * @param setKey    Set操作的Key
     * @param commentEnum 点赞类型枚举
     */
    private void doLike(HashOperations hash, SetOperations set, String userId, String id, String hashKey, String setKey, CommentEnum commentEnum) {
        // 判断用户是否已经点赞
        boolean isLiked = set.isMember(setKey, id);
        if (isLiked) {
            // 取消点赞
            hash.increment(hashKey, _LIKE, -1); // 点赞数减1
            set.remove(setKey, id); // 移除用户点赞记录
        } else {
            // 点赞
            hash.increment(hashKey, _LIKE, 1); // 点赞数加1
            set.add(setKey, id); // 添加用户点赞记录
            // 学习资料类型不发送通知
            if (commentEnum == CommentEnum.STUDYMATERIAL) {
                return;
            }
            // 点赞成功后发送通知
            //异步线程发送通知
            threadPoolExecutor.execute(() -> {
                log.info("异步线程发送通知");
                sendMessageService.doNotice(id, userId, commentEnum,"0");
            });
        }
    }

}
